package edu.zut.main.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class ThreadDemo1 {

    public static void main(String[] args) {
        //创建多条线程对象
        ImgThread imgThreadA = new ImgThread("https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E7%A8%8B%E5%BA%8F%E5%91%98&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=&copyright=&cs=351840435,4135527261&os=3222490124,3821595991&simid=18916785,404594178&pn=21&rn=1&di=42460&ln=895&fr=&fmq=1630898318107_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fimages2017.cnblogs.com%252Fnews%252F66372%252F201708%252F66372-20170829144610187-1031540157.jpg%253FimageMogr2%252Fthumbnail%252F%252175p%252Fformat%252Fjpg%252Fquality%252F50%26refer%3Dhttp%253A%252F%252Fimages2017.cnblogs.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1633490319%26t%3D5c1ab122b13d6ae839bf52843b297231&rpstart=0&rpnum=0&adpicid=0&nojc=undefined","1.jpg");
        ImgThread imgThreadB = new ImgThread("https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E7%A8%8B%E5%BA%8F%E5%91%98&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=&copyright=&cs=4250677112,3305739441&os=635497485,2745873908&simid=4290252295,724957888&pn=28&rn=1&di=142450&ln=895&fr=&fmq=1630898318107_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252F5b0988e595225.cdn.sohucs.com%252Fq_mini%252Cc_zoom%252Cw_640%252Fimages%252F20170929%252Fe28345e8e98b4d7e80cea28586415e47.jpeg%26refer%3Dhttp%253A%252F%252F5b0988e595225.cdn.sohucs.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1633490319%26t%3Dfc08c863448f48b55380914faa2ca29a&rpstart=0&rpnum=0&adpicid=0&nojc=undefined", "2.jpg");
        ImgThread imgThreadC = new ImgThread("https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E7%A8%8B%E5%BA%8F%E5%91%98&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=&copyright=&cs=2542695634,859717730&os=622639366,849909412&simid=3488617212,295713875&pn=36&rn=1&di=151910&ln=895&fr=&fmq=1630898318107_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252F5b0988e595225.cdn.sohucs.com%252Fimages%252F20180123%252F65b5099f76684a0e92af7b0f54063cc3.jpeg%26refer%3Dhttp%253A%252F%252F5b0988e595225.cdn.sohucs.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1633490320%26t%3D62a55fcb3b6bc8266e97a3b675b3c7df&rpstart=0&rpnum=0&adpicid=0&nojc=undefined","3.jpg");
        //让线程就绪，准备执行,让cpu选择执行哪一个
        imgThreadA.start();
        imgThreadB.start();
        imgThreadC.start();

    }

}
/**
 * 线程的创建：
 *  1、继承Thread，重写run方法
 *  2、实现Runnable接口，实现run方法
 *
 *  利用多线程模拟同时从网上下载多个图片
 */

//模拟出个线程能进行下载
class ImgThread extends Thread{

    private String url;
    private String fileName;

    public ImgThread(String url,String fileName){
        this.url = url;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        new ImgDown().down(url,fileName);
        System.out.println("下载了图片："+fileName);
    }
}


//提供的有下载功能
class ImgDown{
    /**
     * 下载 FileUtils.copyURLToFile : 将url资源拷贝到指定的文件
     * @param url 资源路劲
     * @param pathName 保存到本地的路劲
     */
    public void down(String url,String pathName){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(pathName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


